﻿using AutoMapper;
using BackData;
using Domain;
using Infratructure;
using MediatR;
using Server.API.Application.Command.Goods;

namespace Server.API.Application.Handler.Goods
{
    public class GoodsUpdateHandler:IRequestHandler<GoodsUpdateCommand, APIResult<int>>
    {
        private readonly IRepository<GoodsModel> goodsway;
        private readonly ILogger<GoodsUpdateHandler> log;
        private readonly IMapper mapper;

        public GoodsUpdateHandler( IRepository<GoodsModel> goodsway, ILogger<GoodsUpdateHandler> log, IMapper mapper)
        {
            this.goodsway = goodsway;
            this.log = log;
            this.mapper = mapper;
        }
        /// <summary>
        /// 商品修改
        /// </summary>
        /// <param name="request">参数</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>影响行数</returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIResult<int>> Handle(GoodsUpdateCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> res = new APIResult<int>();
           var value = mapper.Map<GoodsModel>(request);
            var result =await goodsway.Updateway(value);
            if(result > 0)
            { 
                log.LogInformation("商品修改成功");
                res.Num = StateCode.OK;
                res.Message = "修改成功";
                res.Data = result;
            }
            else
            {
                log.LogError("商品修改失败");
                res.Num = StateCode.Fail;
                res.Message = "修改失败";
            }
            return res;
            

        }
    }
}
